package smcms.service.sso.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import smcms.commons.api.entity.result.CommonsResult;
import smcms.service.sso.entity.AccountReq;
import smcms.service.sso.entity.StaffInfoReq;
import smcms.service.sso.entity.StaffInfoResp;
import smcms.service.sso.entity.SysMenuResp;
import smcms.service.sso.service.UserLoginService;

import java.util.List;

@RequestMapping(value = "/auth")
@RestController
public class LoginController {
    @Autowired
    private CommonsResult commonsResult;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserLoginService userLoginService;

    @GetMapping("/isTokenExpired")
    public CommonsResult isTokenExpired(String username){
        Object token = redisTemplate.opsForValue().get(username);
        if (ObjectUtils.isEmpty(token)) {
            return commonsResult.success(false);
        }
        return commonsResult.success(true);
    }

    @GetMapping("/hasMenu")
    public CommonsResult hasMenu(String username) {
        List<SysMenuResp> sysMenuResps = userLoginService.hasMenu(username);
        return commonsResult.success(sysMenuResps);
    }

    @PostMapping("/register")
    public CommonsResult register(@RequestBody() AccountReq accountReq){
        userLoginService.UserRegister(accountReq);
        return commonsResult.success();
    }

    @GetMapping("/getUserInfo")
    public CommonsResult getUserInfo(String username){
        StaffInfoResp userInfo = userLoginService.getUserInfo(username);
        return commonsResult.success(userInfo);
    }

    @PostMapping("/updateUserInfo")
    public CommonsResult updateUserInfo(@RequestBody() StaffInfoReq staffInfoReq){
        return userLoginService.updateUserInfo(staffInfoReq);
    }

    @PostMapping("/getStaffList")
    public CommonsResult getStaffList(@RequestBody() StaffInfoReq staffInfoReq){
        PageInfo<StaffInfoResp> userList = userLoginService.queryStaffList(staffInfoReq);
        return commonsResult.success(userList);
    }
    @PostMapping("/getStaffListByRole")
    public CommonsResult getStaffListByRole(@RequestBody() StaffInfoReq staffInfoReq){
        List<StaffInfoResp> staffList = userLoginService.getStaffListByRole(staffInfoReq);
        return commonsResult.success(staffList);
    }
}
